package com.jeesit.cloud.spot.business.impl.Impl;

import com.jeesit.cloud.dto.*;
import com.jeesit.cloud.model.*;
import com.jeesit.cloud.spot.business.impl.IHedgingSchemeBiz;
import com.jeesit.cloud.spot.service.*;
import lombok.extern.slf4j.Slf4j;
import org.apache.poi.ss.formula.functions.T;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@Service
@Slf4j
public class HedgingSchemeBizImpl implements IHedgingSchemeBiz {

    @Autowired
    private IHedgingSchemeService hedgingSchemeService;
    @Autowired
    private IHedgingSchemeInstService hedgingSchemeInstService;
    @Autowired
    private IHedgeSchemeAccountService hedgeSchemeAccountService;
    @Autowired
    private IContractSchemaService contractSchemaService;
    @Autowired
    private IContractService contractService;

    /**
     * 套保方案
     * @param user
     * @param id
     * @param entity
     * @return
     */
    @Transactional
    @Override
    public JSONResult<HedgingScheme> saveData(User user, Long id, HedgingSchemeDto entity) {
        HedgingScheme it = new HedgingScheme();
        if(id==null){
            it.setDeleted(false);
            it.setAddUserName(user.getNickName());
            it.setAddTime(new Date());
            it.setAddUserUuid(user.getId());
        } else {
            it = hedgingSchemeService.getById(entity.getUuid());
            it.setModUserName(user.getNickName());
            it.setModTime(new Date());
            it.setModUserUuid(user.getId());
        }
        it.setIsShow(true);
        it.setBizOrgName(entity.getBizOrgName());
        it.setBizOrgUuid(entity.getBizOrgUuid());
        it.setName(entity.getName());
        it.setBusinessType(entity.getBusinessType());
        it.setSalesmanName(entity.getSalesmanName());
        it.setMaturityDate(entity.getMaturityDate());
        it.setSalesmanUuid(entity.getSalesmanUuid());
        it.setTraderName(entity.getTraderName());
        it.setTraderUuid(entity.getTraderUuid());
        it.setRiskName(entity.getRiskName());
        it.setRiskUuid(entity.getRiskUuid());
        it.setExecutingState(entity.getExecutingState());
        it.setRemark(entity.getRemark());
        it.setCatalog(entity.getCatalog());
        it.setProduct(entity.getProduct());
        it.setInstrumentCode(entity.getInstrumentCode());
        it.setMaxPosition(entity.getMaxPosition());
        it.setSpotWeight(entity.getSpotWeight());
        it.setProfit(entity.getProfit());
        it.setClientProfit(entity.getClientProfit());
        it.setCheckTime(entity.getCheckTime());
        it.setCheckUserName(entity.getCheckUserName());
        it.setCheckUserUuid(entity.getCheckUserUuid());
        hedgingSchemeService.saveOrUpdate(it);
        if(entity.getHedgingSchemeInstDtos()!=null&&entity.getHedgingSchemeInstDtos().size()>0){//套保合约明细
            if(id!=null){//编辑之前先删除
                hedgingSchemeInstService.deltedDate(id);
            }
            List<HedgingSchemeInstDto> hedgingSchemeInstDtos = entity.getHedgingSchemeInstDtos();
            List<HedgingSchemeInst> hedgingSchemeInsts = new ArrayList<>();
            for(HedgingSchemeInstDto its : hedgingSchemeInstDtos){
                HedgingSchemeInst hedgingSchemeInst = new HedgingSchemeInst();
                hedgingSchemeInst.setInstrumentCode(its.getInstrumentCode());
                hedgingSchemeInst.setInstrumentUuid(its.getInstrumentUuid());
                hedgingSchemeInst.setProductName(its.getProductName());
                hedgingSchemeInst.setProductUuid(its.getProductUuid());
                hedgingSchemeInst.setDirection(its.getDirection());
                hedgingSchemeInst.setMaxPosition(its.getMaxPosition());
                hedgingSchemeInst.setPositionType(its.getPositionType());
                hedgingSchemeInst.setMaxPrice(its.getMaxPrice());
                hedgingSchemeInst.setMinPrice(its.getMinPrice());
                hedgingSchemeInst.setPriceType(its.getPriceType());
                hedgingSchemeInst.setPrice(its.getPrice());
                hedgingSchemeInst.setVolumeMultiple(its.getVolumeMultiple());
                hedgingSchemeInst.setSchemeUuid(it.getUuid());
                hedgingSchemeInst.setAddUserName(user.getNickName());
                hedgingSchemeInst.setAddUserUuid(user.getId());
                hedgingSchemeInst.setDeleted(false);
                hedgingSchemeInst.setAddTime(new Date());
                hedgingSchemeInsts.add(hedgingSchemeInst);
            }
            hedgingSchemeInstService.saveOrUpdateBatch(hedgingSchemeInsts);
        }
        if(entity.getHedgeSchemeAccountDtos()!=null&&entity.getHedgeSchemeAccountDtos().size()>0){
            if(id!=null){//编辑之前先删除
                hedgeSchemeAccountService.deltedDate(id);
            }
            List<HedgeSchemeAccount> hedgeSchemeAccounts = new ArrayList<>();
            List<HedgeSchemeAccountDto> hedgeSchemeAccountDtos = entity.getHedgeSchemeAccountDtos();
            for(HedgeSchemeAccountDto its : hedgeSchemeAccountDtos){
                HedgeSchemeAccount hedgeSchemeAccount = new HedgeSchemeAccount();
                hedgeSchemeAccount.setCtpUuid(its.getCtpUuid());
                hedgeSchemeAccount.setSchemeUuid(it.getUuid());
                hedgeSchemeAccount.setAddUserName(entity.getAddUserName());
                hedgeSchemeAccount.setAddUserUuid(entity.getAddUserUuid());
                hedgeSchemeAccount.setDeleted(false);
                hedgeSchemeAccount.setAddTime(new Date());
                hedgeSchemeAccounts.add(hedgeSchemeAccount);
            }
            hedgeSchemeAccountService.saveOrUpdateBatch(hedgeSchemeAccounts);
        }
        if(entity.getContractUuid()!=null&&entity.getContractUuid().length>0){
            if(id!=null){//编辑之前先删除
                contractSchemaService.deltedDate(id);
            }
            List<ContractSchema> contractSchemas = new ArrayList<>();
            for(Long its : entity.getContractUuid()){
                ContractSchema contractSchema = new ContractSchema();
                contractSchema.setAddUserName(user.getNickName());
                contractSchema.setAddUserUuid(user.getId());
                contractSchema.setContractUuid(its);
                contractSchema.setSchemaUuid(it.getUuid());
                contractSchema.setAddTime(new Date());
                contractSchema.setDeleted(Boolean.FALSE);
                contractSchemas.add(contractSchema);
                Contract contract = contractService.getById(its);
                contract.setIsScheme(true);
                contractService.saveOrUpdate(contract);
            }
            contractSchemaService.saveOrUpdateBatch(contractSchemas);
        }
        JSONResult<HedgingScheme> res = new JSONResult<>();
        res.setData(it);
        res.setMessage("操作成功");
        res.setSuccess(true);
        return res;
    }
}
